package com.ktgj.common.core.keyutil.helper;

import com.ktgj.common.core.keyutil.AesUtil;
import com.ktgj.common.core.keyutil.RSAUtil;
import com.ktgj.common.core.keyutil.ZipUtils;
import com.ktgj.common.core.keyutil.model.EncryptMessageModel;

import java.security.interfaces.RSAPublicKey;

/**
 * @resume 加密帮助类
 * @author Johnny
 *
 */
public class EncryptHelper {

	/**
	 * @resunme 请求体加密 
	 * 			a:身份加密采用RSA加密   身份密文 = RSA(毫秒值 + identity + password)
	 * 			b:整体加密 = AES( identity(明文) + 身份密文 + content(请求体) )
	 * 			c:压缩    
	 * @param model
	 * @param publicKey
	 * @param aesPassword
	 * @return
	 * @throws Exception
	 */
	public static final String encrypt(EncryptMessageModel model, RSAPublicKey publicKey, String aesPassword) throws Exception{
		//组装身份信息串
		String identyStr = System.currentTimeMillis() + "<&>" + model.getIdentity() + "<&>" + model.getPassword();
		//加密身份信息
		String identyCode = RSAUtil.encryptByPublicKey(identyStr, publicKey);
		//组装请求体
		String contentStr = model.getIdentity() + "<#>" + identyCode + "<#>" + model.getContent();
		//AES加密
		String code = AesUtil.encrypt(contentStr, aesPassword);
		//加密
		return ZipUtils.gzip(code);
	}
	
}
